package com.byjh.web.controller.app;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.byjh.common.annotation.Log;
import com.byjh.common.core.controller.BaseController;
import com.byjh.common.core.domain.AjaxResult;
import com.byjh.common.enums.BusinessType;
import com.byjh.app.domain.QuestionOption;
import com.byjh.app.service.IQuestionOptionService;
import com.byjh.common.core.page.TableDataInfo;

/**
 * 题目选项Controller
 *
 * @author Dhq
 * @date 2025-01-17
 */
@RestController
@RequestMapping("/app/option")
public class QuestionOptionController extends BaseController {
    @Autowired
    private IQuestionOptionService questionOptionService;

    /**
     * 查询题目选项列表
     */
    @GetMapping("/list")
    public TableDataInfo list(QuestionOption questionOption) {
        startPage();
        List<QuestionOption> list = questionOptionService.selectQuestionOptionList(questionOption);
        return getDataTable(list);
    }

    /**
     * 新增题目选项
     */
    @Log(title = "题目选项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QuestionOption questionOption) {
        return toAjax(questionOptionService.insertQuestionOption(questionOption));
    }

    @Log(title = "题目选项", businessType = BusinessType.INSERT)
    @PostMapping("/addAndEdit")
    public AjaxResult addAndEdit(@RequestBody List<QuestionOption> options) {
        options.forEach(option -> {
            if (option.getId() == null) {
                // 新增
                questionOptionService.insertQuestionOption(option);
                return;
            }
            questionOptionService.updateQuestionOption(option);
        });
        return toAjax(1);
    }

    /**
     * 修改题目选项
     */
    @Log(title = "题目选项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QuestionOption questionOption) {
        return toAjax(questionOptionService.updateQuestionOption(questionOption));
    }

    /**
     * 删除题目选项
     */
    @Log(title = "题目选项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(questionOptionService.deleteQuestionOptionByIds(ids));
    }
}
